package uc108.com.jetpackdemo.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import uc108.com.jetpackdemo.R;
import uc108.com.jetpackdemo.databinding.GamelistItemBinding;
import uc108.com.jetpackdemo.room.Game;

public class GameListAdapter extends RecyclerView.Adapter<GameListAdapter.GameListViewHolder> {

    private List<Game> datas;

    public GameListAdapter(List<Game> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public GameListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        GamelistItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.gamelist_item,
                parent,
                false);
        return new GameListViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull GameListViewHolder holder, int position) {
        holder.binding.setGame(datas.get(position));
        holder.binding.executePendingBindings();
    }


    @Override
    public int getItemCount() {
        return  datas == null ? 0 : datas.size();
    }

    public void setDatas(List<Game> games) {
        this.datas = games;
    }

    class GameListViewHolder extends RecyclerView.ViewHolder {

        private GamelistItemBinding binding;

        public GameListViewHolder(@NonNull GamelistItemBinding itemBinding) {
            super(itemBinding.getRoot());
            this.binding = itemBinding;
        }
    }

}
